import profile
import script.LuaPreprocessor as LuaPreprocessor
import script.LuaMerger as LuaMerger
import helpers.FileHelper as FileHelper
import helpers.MiscHelper as MiscHelper
import checkers.ASCIIChecker as ASCIIChecker
import checkers.ImageChecker as ImageChecker
import checkers.ExtraChecker as ExtraChecker
import checkers.VersionChecker as VersionChecker
from package.PackageInterface import PackageInterface
from package.AmazingFeature import AmazingFeature


class Package(PackageInterface):

    def __init__(self, root_path, conf):
        PackageInterface.__init__(self, root_path, conf)
        self.features = []
        self.is_cc = False
        self.is_xt = False
        single_amaz = MiscHelper.json_get(self.conf, "effect", "AmazingScene")
        if single_amaz is not None:
            self.features.append(AmazingFeature(FileHelper.solve_path(root_path, single_amaz["path"])))
        else:
            for feature_meta in MiscHelper.json_get(self.conf, "effect", "Link"):
                feature_type = feature_meta["type"]
                if feature_type == "AmazingFeature":
                    feature_path = FileHelper.solve_path(self.path, feature_meta["path"])
                    if feature_path.endswith("/"):
                        feature_path = feature_path[:-1]
                    self.features.append(AmazingFeature(feature_path))
            if FileHelper.exists(root_path, "extra.json"):
                extra_data = FileHelper.r_json(root_path, "extra.json")
                self.is_cc = MiscHelper.json_get(extra_data, "setting", "effect_adjust_params") is not None
                self.is_xt = MiscHelper.json_get(extra_data, "settings") is not None

    def setup(self, opts):
        for k in opts:
            v = opts[k]
            if k == "check-ascii":
                ASCIIChecker.check(self.path)
            elif k == "check-image":
                ImageChecker.check(self.path)
            elif k == "check-extra":
                ExtraChecker.check(self.path)
            # elif k == "check-version":
            #     VersionChecker.check(self.conf, profile.effect_min_version)
            elif k == "remove-slider-config":
                self.remove_slider_config()
            elif k in ("D", "define"):
                LuaPreprocessor.preprocess(MiscHelper.parse_macros(v), self.path)
            elif k in ("m", "merge"):
                self.merge()
            elif k in ("b", "binary"):
                for feature in self.features:
                    feature.convert(True)
            elif k in ("t", "text"):
                for feature in self.features:
                    feature.convert(False)
            elif k in ("h", "rehash"):
                if self.is_cc:
                    self.rehash(profile.cc_min_version)
                elif self.is_xt:
                    self.rehash(profile.xt_min_version)
                else:
                    self.rehash(profile.effect_min_version)
            elif k in ("H", "rehash-features"):
                self.rehash_children()
            elif k in ("s", "sign"):
                self.sign()

    def merge(self):
        root_path = FileHelper.solve_path(self.path, "src")
        LuaMerger.merge(root_path, "MainEntry", FileHelper.solve_path(self.path, "event.lua"))
        for feature in self.features:
            feature.merge()

    def rehash_children(self):
        for feature in self.features:
            feature.rehash()

    def remove_slider_config(self):
        for feature in self.features:
            feature.remove_slider_config()
